<?php

/**
 * 失败返回false
 */
/* function curl_post($url, $timeout = 10, array $post = array(), array $options = array())
  {
  $defaults = array(
  CURLOPT_POST => 1,
  CURLOPT_HEADER => 0,
  CURLOPT_URL => $url,
  CURLOPT_FRESH_CONNECT => 1,
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_FORBID_REUSE => 1,
  CURLOPT_TIMEOUT => $timeout,
  CURLOPT_POSTFIELDS => http_build_query($post)
  );
  $ch = curl_init();
  curl_setopt_array($ch, ($options + $defaults));
  $result = curl_exec($ch);
  curl_close($ch);
  return $result;
  } */
function curl_post($url, array $post = array(), $timeout = 30, array $options = array()) {
    $defaults = array(
        CURLOPT_POST => 1,
        CURLOPT_HEADER => 0,
        CURLOPT_URL => $url,
        CURLOPT_FRESH_CONNECT => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FORBID_REUSE => 1,
        CURLOPT_TIMEOUT => $timeout,
        CURLOPT_POSTFIELDS => http_build_query($post)
    );

    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

/**
 * 失败返回false
 */
function curl_get($url, $timeout = 10, array $options = array()) {
    $defaults = array(
        CURLOPT_URL => $url,
        CURLOPT_HEADER => 0,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_NOBODY => FALSE,
        CURLOPT_TIMEOUT => $timeout,
    );
    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

function paracheck($para) {
    foreach ($para as $value) {
        if (!$value) {
            echo 'required para is missing';
            exit;
        }
    }
}

function trimsql($str) {
    $str = str_replace("\"", '', $str);
    $str = str_replace("'", '', $str);
    $str = str_replace("\\", '', $str);
    $str = str_replace("\r", '', $str);
    $str = str_replace("\n", '', $str);
    return $str;
}

function signcheck($para) {
    $msign = $_GET['sign'];
    $str = '';
    foreach ($para as $p) {
        $str.=$p;
    }
    $sign = md5($str . KEY);
    if ($sign != $msign) {
        echo echojson('sign error', 0, '');
        exit;
    }
}

function openlinksign() {
    global $uid;
    global $imei;
    global $ver;
    $uri = strstr($_SERVER['REQUEST_URI'], "&appid", TRUE);
    $signstr = substr($uri, -6);
    $msign = $_GET['sign'];
    $str = $signstr.$uid.$imei.$ver;
    $sign = md5($str . KEY);
    if ($sign != $msign) {
        echo echojson('sign error', 0, '');
        exit;
    }
}

function logincheck() {
    global $uid;
    if (!$uid) {
        echo echojson('please login', 0, '');
        exit;
    }
}

function busy() {
    header("Content-type: text/html; charset=UTF 8");
    echo "<!DOCTYPE html><html><meta http-equiv='X-UA-Compatible' content='IE=edge'><meta name='viewport' content='initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, width=device-width'></head><body>";
    echo "<h2 style='text-align:center'>系统繁忙,请稍后再试</h2>";
    echo "</body></html>";
    exit;
}

function doadminlog($action = '', $datas = '') {
    $username = getnadminname();
    $ctime = time();
    if (is_array($datas)) {
        unset($datas['ac']);
        unset($datas['op']);
        $datas = serialize($datas);
    }
    exec_db("INSERT INTO sys_adminlog (username,action,datas,ctime) VALUES ('{$username}','{$action}','{$datas}','{$ctime}')");
}



function isMobile($phone) {
    $regex = '/^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$/';
    $matches = array();
    if (preg_match($regex, $phone, $matches)) {
        return true;
    }
    return false;
}

/**
 * 获取用户图像
 */
function getuserlogo($logo) {
    if (!$logo || $logo == 'null' || $logo == '(null)') {
        return HOST . '/static/images/default_user_logo.png';
    }
    return $logo;
}

function getUserInfo($uid) {
    $mkey = "qmdj_userinfo_$uid";
    $memcache_obj = get_memcache(0);
    $userinfo = memcache_get($memcache_obj, $mkey);
    if ($userinfo === false) {
        $userinfo = getonerecord("select logo,username,phone from userinfo where id='$uid' limit 1");
        memcache_add($memcache_obj, $mkey, $userinfo, false, 60 * 60 * 24 * 15);
    }
    $userinfo['logo'] = getuserlogo($userinfo['logo']);
    return $userinfo;
}

function updateUserinfo($uid, $data) {
    $mkey = "qmdj_userinfo_$uid";
    $memcache_obj = get_memcache(0);
    $userinfo = memcache_get($memcache_obj, $mkey);
    if ($userinfo === false) {
        $userinfo = $data;
    } else {
        if (!$data) {
            $data = array();
        }
        if (!$userinfo) {
            $userinfo = array();
        }
        $userinfo = array_merge($userinfo, $data);
    }
    memcache_set($memcache_obj, $mkey, $userinfo, false, 60 * 60 * 24 * 15);
}

function time_tran($the_time) {
    $dur = time() - $the_time;
    if ($dur < 0) {
        return $the_time;
    } else {
        if ($dur < 60) {
            if ($dur == 0) {
                $dur = 1;
            }
            return $dur . '秒前';
        } else {
            if ($dur < 3600) {
                return floor($dur / 60) . '分钟前';
            } else {
                if ($dur < 86400) {
                    return floor($dur / 3600) . '小时前';
                } else {
                    if ($dur < 86400 * 30) { // 3天内
                        return floor($dur / 86400) . '天前';
                    } else {
                        return '1月前';
                    }
                }
            }
        }
    }
}

function getreqlog($filename = '', $content = '') {
    if (empty($filename)) {
        $filename = 'reqlog';
    }
    if (empty($content)) {
        $content = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] . "\r\n";
    }
    file_put_contents(S_ROOT . "cache/{$filename}.txt", $content, FILE_APPEND);
}

?>